﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;
using YJKRGeometry;
using ModelCheckUtility.ControlLayer;

namespace ModelCheckUtility.ModelLayer
{
	public class BaseFamilyInstanceBoundaryPacker
	{
		public FamilyInstance ThisFamilyInstance
		{
			get
			{
				return this.m_thisFamilyInstance;
			}
		}

		public RevitLinkInstance ThisUseLinke
		{
			get
			{
				return this.m_thisUseLink;
			}
		}

		public Transform UseTransForm
		{
			get
			{
				return this.m_useTransForm;
			}
		}

		public ElementGeometryPacker ThisElementGeomentryPacker
		{
			get
			{
				return this.m_thisElementGeomentryPacker;
			}
		}

		public XYZ UseLocationPoint
		{
			get
			{
				return this.m_useLocationPoint;
			}
		}

		public XYZ UseLocationPointZ
		{
			get
			{
				return this.m_useLocationPointZ;
			}
		}

		public double UseWidthValue
		{
			get
			{
				return this.m_useWidthValue;
			}
		}

		public List<Curve> LstUseCurves
		{
			get
			{
				return this.m_lstUseCurves;
			}
		}

		public double UseZValue
		{
			get
			{
				return this.m_dUseZValue;
			}
		}

		public Level UseLevel
		{
			get
			{
				return this.m_useLevel;
			}
		}

		public BaseFamilyInstanceBoundaryPacker(FamilyInstance useFamiyInstance, List<Level> lstAllLevels, ElementGeometryPacker tempGeometryPacker, RevitLinkInstance useLinkInstance)
		{
			this.m_thisFamilyInstance = useFamiyInstance;
			if (useLinkInstance != null)
			{
				this.m_thisUseLink = useLinkInstance;
				this.m_useTransForm = useLinkInstance.GetTotalTransform();
			}
			this.m_thisElementGeomentryPacker = tempGeometryPacker;
			this.m_useLocationPoint = this.m_thisElementGeomentryPacker.CenterPoint;
			bool ifUseCenter = useFamiyInstance.Category.Id.IntegerValue == -2000023;
			this.m_useLevel = LinkDocumentUtility.FindFitLevel(this.m_thisElementGeomentryPacker, lstAllLevels, ifUseCenter);
			this.m_lstUseCurves = Utility.GetBoundaryCurves(this.m_thisFamilyInstance, out this.m_useWidthValue, this.UseTransForm, this.ThisElementGeomentryPacker, 0);
			if (this.m_useLevel != null)
			{
				this.m_dUseZValue = this.m_useLevel.Elevation;
				this.m_useLocationPointZ = new XYZ(this.UseLocationPoint.X, this.UseLocationPoint.Y, this.UseZValue);
				if (this.m_lstUseCurves != null)
				{
					List<Curve> lstUseCurves = this.m_lstUseCurves;
					this.m_lstUseCurves = new List<Curve>();
					foreach (Curve inputCurve in lstUseCurves)
					{
						this.m_lstUseCurves.Add(Utility.TransformCurveAtZAixs(inputCurve, this.m_dUseZValue, true));
					}
				}
			}
		}

		public bool IfCurveIntersectionThis(Curve inputCurve)
		{
			Utility.CurveCheck(inputCurve);
			if (this.m_useLevel == null)
			{
				return false;
			}
			double z = inputCurve.GetEndPoint(0).Z;
			if (!Geometry.IsEqual(z, this.UseZValue))
			{
				return false;
			}
			if (!Geometry.Lessthan_Or_Equal(inputCurve.Distance(this.UseLocationPointZ), this.UseWidthValue))
			{
				return false;
			}
			bool flag = true;
			foreach (Curve curve in this.LstUseCurves)
			{
				SetComparisonResult setComparisonResult = inputCurve.Intersect(curve);
                flag = (8 == (int)setComparisonResult && flag);
			}
			return flag;
		}

		public bool TryGetBaseElementPacker(out BaseElementPacker thisPacker)
		{
			thisPacker = null;
			bool result;
			try
			{
				thisPacker = ElementPackerManger.FindSameBaseElementPacker(this.ThisFamilyInstance, this.ThisUseLinke);
				if (thisPacker == null)
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		private FamilyInstance m_thisFamilyInstance;

		private RevitLinkInstance m_thisUseLink;

		private Transform m_useTransForm;

		private ElementGeometryPacker m_thisElementGeomentryPacker;

		private XYZ m_useLocationPoint;

		private XYZ m_useLocationPointZ;

		private double m_useWidthValue = -1.0;

		private List<Curve> m_lstUseCurves;

		private double m_dUseZValue;

		private Level m_useLevel;
	}
}
